package com.windfone.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;


public class ComposeDateIntArrays {

	public static String getBackDateByPoint(String currentDatey,int days){
		String rtvDate = "";
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
	    try {
	        Date date = f.parse(currentDatey);
	        Calendar calendar = GregorianCalendar.getInstance();
	        calendar.setTime(date);
	        calendar.set(Calendar.HOUR, 0);
	        calendar.set(Calendar.MINUTE, 0);
	        calendar.set(Calendar.SECOND, 0);
	        calendar.add(Calendar.DATE, -days);
	        rtvDate = f.format( calendar.getTime() );
	    } catch (ParseException e) {
	        e.printStackTrace();
	    }
	    return rtvDate;
	}
	
	public static String changeDateToStr(Date date){
		String rtvDate = "";
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		try {
			rtvDate = f.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rtvDate;
	}
	public static String getBackDateByPoint(Date currentDatey,int days){
		String rtvDate = "";
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		try {
//			Date date = f.parse(currentDatey);
			Calendar calendar = GregorianCalendar.getInstance();
			calendar.setTime(currentDatey);
			calendar.set(Calendar.HOUR, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			calendar.add(Calendar.DATE, -days);
			rtvDate = f.format( calendar.getTime() );
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rtvDate;
	}
	
	public static Date changeStrToDate(String dateStr){
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		Date rtv = null;
		try {
			rtv = f.parse(dateStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rtv;
	}
	public static String splitDateStr(String dateStr){
		String rtv = "";
		String[] dateArrays = dateStr.split("\\.");
	    if ( dateArrays.length == 3 ){
        	String year= dateArrays[0];
        	String month= dateArrays[1];
        	String day= dateArrays[2];
        	if ( year.length() == 4){
        		if ( month.length()==1 ){
        			month = "0"+month;
        		}
        		if ( day.length() == 1 ){
        			day =  "0" + day;
        		}
        		rtv = year + "-" + month + "-"+day;
        	}
        }
        return rtv;
	}
    /**
     * @param args
     */
    public static void main(String[] args) {
        String beginDate = "2007-12-27";
        String endDate = "2009-01-02";
        Integer [] days = getDateFromNumber( beginDate,endDate);
        for(int i=0;i<days.length;i++){
            System.out.println( days[i]);
        }
        String dateStr = "2010.8.21";
        System.out.println(  splitDateStr(dateStr));
    }
    
    
    public static int  getMonthDays( int year ,int month ){
        int rtv = 0;
        int[] month31 = {1,3,5,7,8,10,12};
        int[] month30 = {4,6,9,11};
        int month2 = 28;
        if ( (year%4 ==0 && year %100!=0) || (year %400==0)){
            month2 = 29;
        }
        for( int i=0;i<month30.length;i++){
            if ( month30[i] == month ){
                rtv = 30;
                break;
            }
        }
        for( int i=0;i<month31.length;i++){
            if ( month31[i] == month ){
                rtv = 31;
                break;
            }
        }
        if ( month == 2)
            rtv = month2;
        return rtv;
    }
    
    public static Integer[] getDateFromNumber(String beginDate,String endDate ){
        int byear = Integer.parseInt( beginDate.substring(0, 4) );
        int bmonth = Integer.parseInt( beginDate.substring(5, 7) );
        int bday = Integer.parseInt( beginDate.substring(8, 10) );
        int eyear = Integer.parseInt( endDate.substring(0, 4) );
        int emonth = Integer.parseInt( endDate.substring(5, 7) );
        int eday = Integer.parseInt( endDate.substring(8, 10) );
        List<Integer> rtvLs =  new ArrayList<Integer>();
        for(int i=0;i<=(eyear-byear);i++  ){
            int yearSub = eyear - byear;
            if ( yearSub== 0    ){ //如果是同年
                for(int j=0;j<=(emonth-bmonth);j++){ 
                    int monthSub = emonth - bmonth;
                    if ( monthSub==0 ){ //如果是同年同月
                        for(int k=0;k<=(eday-bday);k++){ //处理同年同月的日期
                            int enlargeYear = eyear*10000;
                            int enlargeMonth = emonth*100;
                            int enlargeDay = bday+k;
                            int inputData = enlargeYear+ enlargeMonth + enlargeDay;
                            rtvLs.add(  inputData );
                        }
                    } else {//如果是同年，但不是同月
                        int currentYear = eyear;
                        int currentMonth = bmonth+j;
                        //判断开始处理的那个月有多少天
                        int currentMonthDays = getMonthDays( currentYear,currentMonth );
                        System.out.println( currentYear + ":"+currentMonth + ":" +currentMonthDays);
                        if ( currentMonth < emonth  ){ //处理结束月之前的
                            int startDay = 0; 
                            if ( j == 0 )//当月的天数
                                startDay = bday ; 
                            else 
                                startDay = 1 ;
//                            for(int k=0;  k<(currentMonthDays-startDay );  k++ ){
                            for(int k=startDay;  k<=currentMonthDays;  k++ ){
                                int enlargeYear = eyear*10000;
                                int enlargeMonth =  currentMonth *100;
                                int enlargeDay = 0;
                                enlargeDay = k;
                                int inputData = enlargeYear+ enlargeMonth + enlargeDay;
                                rtvLs.add(  inputData );
                            }
                        } else {
                            for(int k=0;k<eday;k++){ 
                                int enlargeYear = eyear*10000;
                                int enlargeMonth = emonth*100;
                                int enlargeDay = k+1;
                                int inputData = enlargeYear+ enlargeMonth + enlargeDay;
                                rtvLs.add(  inputData );
                            }
                        }
                    }
                }
            } else {//如果不同年
                if ( i ==0 ) { //处理当年所有月份
                    for(int j=bmonth;j<=12;j++){
                        int currentYear = byear;
                        int currentMonth = j;
                        //判断开始处理的那个月有多少天
                        int currentMonthDays = getMonthDays( currentYear,currentMonth);
                        int startDay = 0; 
                        if ( j == bmonth )//当月的天数
                            startDay = bday ; 
                        else 
                            startDay = 0 ;
                        for(int k=startDay;k<=currentMonthDays;k++ ){
                            int enlargeYear = currentYear*10000;
                            int enlargeMonth =  currentMonth *100;
                            int enlargeDay = k;
                            int inputData = enlargeYear+ enlargeMonth + enlargeDay;
                            rtvLs.add(  inputData );
                        }
                    }
                }
                if ( i ==(eyear-byear)) { //最后一年的
                    for(int j=0;j<emonth;j++){
                        int currentYear = eyear;
                        int currentMonth = j+1;
                         
                        //判断开始处理的那个月有多少天
                        int currentMonthDays = getMonthDays( currentYear,currentMonth);
                        if ( currentMonth == emonth )//当月的天数
                        	currentMonthDays = eday ; 
                        for(int k=0;k<currentMonthDays;k++ ){
                            int enlargeYear = currentYear*10000;
                            int enlargeMonth =  currentMonth *100;
                            int enlargeDay = k+1;
                            int inputData = enlargeYear+ enlargeMonth + enlargeDay;
                            rtvLs.add(  inputData );
                        }
                    }
                }
                if (  i !=(eyear-byear)  &&   i !=0) { //中间年份
                    for(int j=0;j<12;j++){
                        int currentYear = byear+i;
                        int currentMonth =j+1;
                        //判断开始处理的那个月有多少天
                        int currentMonthDays = getMonthDays( currentYear,currentMonth);
                        for(int k=0;k<currentMonthDays;k++ ){
                            int enlargeYear = currentYear*10000;
                            int enlargeMonth =  currentMonth *100;
                            int enlargeDay = k+1;
                            int inputData = enlargeYear+ enlargeMonth + enlargeDay;
                            rtvLs.add(  inputData );
                        }
                    }
                }
            
                
            }
        }
        Integer [] rtvArrays =  new Integer[rtvLs.size()];
        rtvLs.toArray( rtvArrays );
        return rtvArrays;
    }

}